﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Script.UI;
using UnityEngine.UI;
using XCharts;
using Script.UI.Component;
using LitJson;
using MTUnity.Utils;
using System;


public class UI_Visitors : UIUpdateBase
{
    public HistogramGroup Panel_7D;
    public HistogramGroup Panel_14D;

    public Text Txt_Guest;
    public Text Txt_Staff;
    public Text Txt_Car;
    public Text Txt_TotalVisitor;
    public Text Txt_RefreshTime;


    public Slider Sld_Guest;
    public Slider Sld_Staff;
    public Slider Sld_Car;

    #region Interface
    public override Dictionary<string, UpdateDel> RegisterProcessorMethod()
    {
        return new Dictionary<string, UpdateDel>
        {
            {JsonFildName.OverView.Current_Visitor , UpdateCurrentVisitor },
            {JsonFildName.OverView.Record_Visitor , UpdateRecordVisitor },
            {JsonFildName.OverView.VisitorRefreshTime , UpdateRefreshTime },
        };
    }
	#endregion

	#region UpdateInterface
    public void UpdateCurrentVisitor(JsonData jsonData)
    {
        UpdateCurrentVisitor(
            JsonUtils.ToInt(jsonData[JsonFildName.OverView.VisitorCount]),
            JsonUtils.ToInt(jsonData[JsonFildName.OverView.GuestVisitorCount]),
            JsonUtils.ToInt(jsonData[JsonFildName.OverView.StaffVisitorCount]),
            JsonUtils.ToInt(jsonData[JsonFildName.OverView.CarVisitorCount])
            );
    }

    public void UpdateRecordVisitor(JsonData jsonData)
    {
        if (jsonData.IsArray)
        {
            List<HistogramDataBase> listData = Utils.SetData<HistogramData_Visitor>(jsonData, JsonFildName.OverView.VisitorCount , 7);
            UpdateRecordVisitor(listData);
        }
    }

    public void UpdateRefreshTime(JsonData jsonData)
    {
        UpdateRefreshTime(JsonUtils.ToLong(jsonData));
    }
    #endregion


    public void UpdateCurrentVisitor(int iVisitorCount , int iGuestCount , int iStaffCount , int iCarCount)
    {
        Txt_TotalVisitor.text = string.Format("{0:N}", iVisitorCount);
        Txt_Guest.text = string.Format("{0:N}", iGuestCount);
        Txt_Staff.text = string.Format("{0:N}", iStaffCount);
        Txt_Car.text = string.Format("{0:N}", iCarCount);

        Sld_Guest.value = (float)iGuestCount / (float)iVisitorCount;
        Sld_Staff.value = (float)iStaffCount / (float)iVisitorCount;
        Sld_Car.value = (float)iCarCount / (float)iVisitorCount;
    }

    public void UpdateRecordVisitor(List<HistogramDataBase> list)
    {
        Panel_7D.Refresh(list);
        Panel_14D.Refresh(list);
    }

    public void UpdateRefreshTime(long lLongTime)
    {
        DateTime datatime = TimeUtil.FromTimestamp(lLongTime);
        Txt_RefreshTime.text = string.Format("数据更新 {0}" , datatime.ToString("{yyyy-MM-dd HH:mm:ss}"));
    }
}